﻿using DG.Tweening;
using System;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class GameUI : UIBase
{
    [SerializeField] private TMP_Text _timerText;
    [SerializeField] private TMP_Text _bombCountText;
    [SerializeField] private Image _mask;
    [SerializeField] private float _maskDuration;

 
    public override void OnOpen(Action onComplete)
    {
        this.gameObject.SetActive(true);
        UpdateTimerValue((int)TimeManager.Instance.TimeLeft);
        UpdateBombCountValue((int)GameMgr.Instance.CurrentPlayer.BombCount);
        GameMgr.Instance.CurrentPlayer.OnBombCountChanged += OnBombCountChanged;
        TimeManager.Instance.OnTimerChanged += OnTimerChanged;
    
    }

    private void OnBombCountChanged(int value)
    {
        UpdateBombCountValue(value);
    }

    private void OnTimerChanged(float value)
    {
        UpdateTimerValue((int)value);    
    }

    private void UpdateTimerValue(int timerText) 
    {
        _timerText.text = timerText.ToString(); 
    }
    private void UpdateBombCountValue(int bombCountText) 
    {
        _bombCountText.text = bombCountText.ToString();
    }

    public override void OnClose(Action onComplete)
    {
        this.gameObject.SetActive(false);
        GameMgr.Instance.CurrentPlayer.OnBombCountChanged -= OnBombCountChanged;
        TimeManager.Instance.OnTimerChanged -= OnTimerChanged;
        var sequence=DOTween.Sequence();
        sequence.Append(_mask.DOFade(1, _maskDuration));
        sequence.AppendCallback(()=>onComplete.Invoke());
        sequence.Play();
        
    }
}

